﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.DataControls;
using CarbonExchange.Bll;

public partial class Admin_Sections : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ds.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(ds_Select);
        ds.Insert += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Insert);
        ds.Update += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Update);
        ds.Delete += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Delete);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        grid.SortCommand += new Telerik.Web.UI.GridSortCommandEventHandler(grid_SortCommand);
    }

    void grid_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        if (e.CommandArgument == "Sequence")
        {
            e.Canceled = true;
            e.Item.OwnerTableView.SortExpressions.Clear();
            e.Item.OwnerTableView.Rebind();
        }
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            int step = e.CommandName == "MoveUp" ? -1 : 1;
            int index = e.Item.DataSetIndex;
            GetFactory().Move(Sections, index, index + step);
            grid.SelectedIndexes.Clear();
            grid.Rebind();
            grid.MasterTableView.Items[e.Item.ItemIndex + step].Selected = true;
        }
    }

    void ds_Delete(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        int id = (int)e.Keys["Id"];
        Section section = Sections.Find(x => x.Id == id);
        if (section != null)
        {
            GetFactory().Delete(section);
            Sections.Remove(section);
        }
    }

    void ds_Update(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        if (IsValid)
        {
            int id = (int)e.Keys["Id"];
            Section section = Sections.Find(x => x.Id == id);
            if (section != null)
            {
                section.Name = e.NewValues["Name"] as string;
                GetFactory().Save(section);
                e.RowsChanged = 1;
            }
        }
    }

    void ds_Insert(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        if (IsValid)
        {
            Section section = new Section();
            section.Name = e.NewValues["Name"] as string;
            GetFactory().Save(section);
            if (_Sections != null)
            {
                Sections.Add(section);
            }
            e.RowsChanged = 1;
        }
    }

    private SectionFactory _Factory;
    protected SectionFactory GetFactory()
    {
        if(_Factory == null)
            _Factory = new SectionFactory();
        return _Factory;
    }

    private List<Section> _Sections;
    protected List<Section> Sections
    {
        get 
        {
            if (_Sections == null)
            {
                _Sections = GetFactory().GetAll().ToList();
            }
            return _Sections;
        }
    }
    void ds_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Sections);
    }
}
